package world0.menu;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.Audio;
import org.newdawn.slick.openal.AudioLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Button {
	
	boolean buttonPressed;
	boolean buttonAction;
	
	/* Image */
	private int ImagePositionX;
	private int ImagePositionY;
	
	private Image image;
	
	private int imageWidth;
	private int imageHeight;
	
	/* Audio */
	Audio audioButtonOver;
	Audio audioButtonPressed;
	
	boolean buttonSoundPlay = false;

	public Button (int x, int y, String textureString, String audioOver, String audioPressed, GameContainer gc) throws SlickException {
		
		/* Create image */
		this.image = new Image(textureString);
		
		imageWidth = image.getWidth();
		imageHeight = image.getHeight();
		
		ImagePositionX = x;
		ImagePositionY = y;
		
		/* Create audio */
		try {
			audioButtonOver = AudioLoader.getAudio("OGG", ResourceLoader.getResourceAsStream(audioOver));
			audioButtonPressed = AudioLoader.getAudio("OGG", ResourceLoader.getResourceAsStream(audioPressed));
		} catch   (Throwable e) { 
			e.printStackTrace();}
	}
	
	public void draw() {
		
		image.drawCentered(ImagePositionX, ImagePositionY);
	}
	
	public boolean buttonAction(GameContainer gc) {
		
		Input input = gc.getInput();
		
		int mouseX = input.getMouseX();
		int mouseY = input.getMouseY();
		
		
		
		if (mouseX >= (ImagePositionX - imageWidth / 2)
				&& mouseX <= (ImagePositionX - imageWidth / 2) + imageWidth
					&& mouseY >= (ImagePositionY - imageHeight /2) 
						&& mouseY <= (ImagePositionY - imageHeight /2) + imageHeight) {
			
			/* Music over effect */
			if (!buttonSoundPlay) {
				buttonSoundPlay = true;
				audioButtonOver.playAsSoundEffect(1.0f, 1.0f, false);
			}
		buttonAction = true;
			
		} else {
			buttonSoundPlay = false;
			buttonAction = false;
		}
		return buttonAction;
	}
	
	/* Button pressed effect*/
	public boolean buttonPressed(GameContainer gc) {
		Input input = gc.getInput();
		
		if (buttonAction(gc) && input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
			buttonPressed = true;
			audioButtonPressed.playAsSoundEffect(1.0f, 1.0f, false);
		} else {
			buttonPressed = false;
		}
		return buttonPressed;
	}

	public void buttonPlayButtonPressed() {
		audioButtonPressed.playAsSoundEffect(1.0f, 1.0f, false);
	}
	
	public void buttonPlayButtonOver() {
		audioButtonOver.playAsSoundEffect(1.0f, 1.0f, false);
	}
	
	public int getPositionX() {
		return ImagePositionX;
	}
	
	public int getPositionY() {
		return ImagePositionY;
	} 
}
